<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="queryList" @reset="() => (formState = {})">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="mcbh">
          <a-input v-model:value="formState.mcbh" class="w-64" placeholder="编号/名称/助记码" />
        </a-form-item>
        <a-form-item name="xmlb" label="项目类别">
          <a-select
            v-model:value="formState.xmlb"
            :max-tag-text-length="3"
            max-tag-count="responsive"
            :virtual="false"
            class="!w-64"
            :options="options"
            mode="multiple"
            placeholder="请选择"
          />
        </a-form-item>
        <a-form-item name="checkbox" class="ml-4">
          <a-checkbox v-model:checked="formState.yxzt">未停用项目</a-checkbox>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <BaseExport type="运维" name="医嘱字典" code="00001050" :search-params="formState">导出</BaseExport>

        <!-- <a-button @click="onExport">导出</a-button> -->
      </div>
      <BaseTable
        :loading="loading"
        column-code="00001050"
        fill
        :data-source="tableData"
        bordered
        :scroll="{ x: 2200 }"
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'dj'">
            {{ record.dj?.toFixed(3) }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg }" />
          </template>
          <template v-if="column.dataIndex === 'yxztStr'">
            <a-switch v-model:checked="record.yxzt" checked-value="0" un-checked-value="1" @change="onClickStatus(record.bh, $event)" />
          </template>
          <template v-if="column.dataIndex === 'jklbStr'">
            <div v-if="record.jklb === 1">医技项目</div>
            <div v-else-if="record.jklb === 4">检验项目</div>
            <div v-else-if="record.jklb === 3">理疗项目</div>
            <div v-else>普通项目</div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickPlace(record)">地点</a-button>
            <a-button type="link" size="small" @click="onAdditional(record)">附加项目</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <PlaceModal v-model:open="openPlace" :row-data="rowData" @success="queryList" />
    <AdditionalModal v-model:open="openAdditional" :row-data="rowData" @success="queryList" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import PlaceModal from './PlaceModal.vue'
import AdditionalModal from './AdditionalModal.vue'

const loading = ref(false)
const tableData = ref([])
const openPlace = ref(false)
const openAdditional = ref(false)
const rowData = ref([])
const formRef = ref(null)
const formState = ref({})
const options = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })

const getProjectList = async () => {
  const { code, data } = await post('/drugStorage/comprehenive/getMedicalProjectList')
  if (code !== 0) return
  options.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.mc
    }
  })
}
const onClickStatus = async (bh) => {
  const { code, msg } = await post('/operation/opex/doConfirmClinicalStatus', { bh })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}
const onClickPlace = (record) => {
  rowData.value = record
  openPlace.value = true
}
const onAdditional = (record) => {
  rowData.value = record
  openAdditional.value = true
}

const queryList = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/operation/opex/getClinicalDictInfoList', {
    mcbh: formState.value.mcbh,
    pageNum: page.value.current,
    pageSize: page.value.pageSize,
    xmlb: formState.value.xmlb,
    yxzt: formState.value.yxzt ? 0 : ''
  })
  if (code !== 0) return message.error(msg)
  tableData.value = data.map((item) => {
    item.yxzt = item.yxzt ? '1' : '0'
    return item
  })
  page.value.total = total
  loading.value = false
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
onMounted(() => {
  getProjectList()
  queryList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
